import asyncio
from aiocoap import *
from aiocoap.resource import Resource, Site

class TemperatureResource(Resource):
    async def render_get(self, request):
        payload = b'Temperature: 22.5C'
        return Message(payload=payload)
    async def render_post(self, request):
        payload = request.payload.decode('utf-8')
        print(f"Received POST request with payload: {payload}")
        return Message(code=CHANGED, payload=b'I am PC: POST request received')

async def main():
    # 创建资源
    root = Site()
    root.add_resource(['qlink', 'success'], TemperatureResource())

    # 创建CoAP服务器，绑定到所有接口
    await Context.create_server_context(root, bind=("172.20.10.7",5683))

    # 保持服务器运行
    await asyncio.get_running_loop().create_future()

if __name__ == "__main__":
    asyncio.run(main())
